import { Vec3, Node, _decorator } from "cc";
const { ccclass, property } = _decorator;

export class Tools {

    /**
     * 判断两个物体是否再区间相交
     */
    public static isIntersect(_node0: Node, _node1: Node, _hor: number, _ver: number): boolean {

        let pos0: Vec3 = _node0.getWorldPosition();
        let pos1: Vec3 = _node1.getWorldPosition();

        if (Math.abs(pos0.x - pos1.x) < _hor && Math.abs(pos0.y - pos1.y) < _ver) {
            return true;
        }

        return false;
    }

    /**
     * 判断两个物体是否再区间相交
     */
    public static isIntersect1(_pos0: Vec3, _node1: Vec3, _hor: number, _ver: number): boolean {

        let pos0: Vec3 = _pos0
        let pos1: Vec3 = _node1

        if (Math.abs(pos0.x - pos1.x) < _hor && Math.abs(pos0.y - pos1.y) < _ver) {
            return true;
        }

        return false;
    }

    /**
    * 数字转时间格式
    * @param sec 
    * @returns 
    */
    static secondsFormat(sec, havehour: boolean) {
        let hour = Math.floor(sec / 3600);
        let minute = Math.floor((sec - hour * 3600) / 60);
        let second = sec - hour * 3600 - minute * 60;
        let hour1 = ''
        let minute1 = ''
        let second1 = ''
        if (hour < 10) {
            hour1 = "0" + hour;
        } else {
            hour1 = "" + hour;
        }
        if (minute < 10) {
            minute1 = "0" + minute;
        } else {
            minute1 = "" + minute;
        }
        if (second < 10) {
            second1 = "0" + second;
        } else {
            second1 = "" + second;
        }
        if (havehour) {
            return hour1 + "：" + minute1 + "：" + second1;
        } else {
            return minute1 + "：" + second1;
        }
    }

    /**
     * 判断两个圆形是否相交
     * @param center0 圆心0
     * @param radius0 半径0
     * @param center1 圆心1
     * @param radius1 半径1
     * @returns 是否相交
     */
    public static isCircleIntersect(center0: Vec3, radius0: number, center1: Vec3, radius1: number): boolean {
        const distance = center0.subtract(center1).length(); // 计算圆心之间的距离
        return distance < (radius0 + radius1); // 判断距离是否小于两个半径之和
    }

    /**
     * 根据两个点返回角度
     */
    public static ReturnAngle(_pos0: Vec3, _pos1: Vec3): number {
        return Math.atan2(_pos1.y - _pos0.y, _pos1.x - _pos0.x) * 180 / Math.PI;
    }

    /**
     * 取随机数
     */
    static GetRandomNum(_Min, _Max): number {
        var Range = _Max - _Min;
        var Rand = Math.random();
        return (_Min + Math.floor(Rand * Range));
    }

}


